package cn.itcast.scm.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.itcast.scm.entity.SysParam;
import cn.itcast.scm.service.SysParamService;
import cn.itcast.scm.utils.SpringContextUtil;

public class SysInitServlet extends HttpServlet {

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the GET method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the POST method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// // Put your code here
		// Map<String, Object> sysParamMap = new HashMap<String, Object>();
		//
		// Map<String, Object> supTypeMap = new HashMap<String, Object>();
		// supTypeMap.put("1", "一级供应商");
		// supTypeMap.put("2", "二级供应商");
		// supTypeMap.put("3", "三级供应商");
		//
		// sysParamMap.put("supType", supTypeMap);
		//
		// Map<String, Object> goodsColorMap = new HashMap<String, Object>();
		// goodsColorMap.put("1", "红色");
		// goodsColorMap.put("2", "绿色");
		// goodsColorMap.put("3", "蓝色");
		// sysParamMap.put("goodsColor", goodsColorMap);
		//
		// this.getServletContext().setAttribute("sysParam", sysParamMap);
		//
		// System.out.println("-------------------系统参数加载成功---------------------");

		SysParamService sysParamService = (SysParamService) SpringContextUtil.getBean("sysParamService");

		Map<String, Object> sysParam = new HashMap<String, Object>();
		Map<String, String> supType = new HashMap<String, String>();
		// 1. 处理供应商类型
		// 1.1 从数据库查出供应商类型
		String sysParamField = "supType";
		List<SysParam> supTypes = sysParamService.selectByParamField("supType");
		// 1.2 把供应商类型放到map
		for (SysParam sup : supTypes) {
			System.out.println("bbbbbbbbbbbbb" + sup);
			supType.put(sup.getSysParamText(), sup.getSysParamValue());
		}
		// 1.3 把map放到sysParam中
		sysParam.put("supType", supType);

		Map<String, String> goodsColor = new HashMap<String, String>();

		List<SysParam> goodsColors = sysParamService.selectByParamField("goodsColor");
		for (SysParam g : goodsColors) {
			goodsColor.put(g.getSysParamText(), g.getSysParamValue());
		}
		sysParam.put("goodsColor", goodsColor);

		ServletContext application = this.getServletContext();
		application.setAttribute("sysParam", sysParam);
		System.out.println("=======初始化完成，已加载数据字典数据=======");
	}
}